package com.aaa.advice;


import com.aaa.mapper.OperateLogMapper;
import com.aaa.pojos.OperateLog;
import com.aaa.util.EmployeeContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.annotation.Retention;
import java.time.LocalDateTime;
import java.util.Arrays;

@Component
@Aspect
public class LogAspect {
    @Resource
    public OperateLogMapper operateLogMapper;

    /**
     * 将当前成员操作存入数据库日志中
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.aaa.service.Impl.*.*(..)) " +
            "&& !execution(* com.aaa.service.Impl.*.query*(..))"
          +"&& !execution(* com.aaa.service.Impl.*.page*(..))"
          +  "&& !execution(* com.aaa.service.Impl.*.job*(..))"
    )
    public Object loginsert(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = proceedingJoinPoint.proceed();
        long end = System.currentTimeMillis();
        OperateLog operateLog=new OperateLog();
        Signature signature = proceedingJoinPoint.getSignature();
        operateLog.setOperateEmpId(EmployeeContext.getEmployeeId());
        operateLog.setOperateTime(LocalDateTime.now());
        operateLog.setClassName(signature.getDeclaringTypeName());
        operateLog.setMethodName(signature.getName());
        operateLog.setMethodParams(Arrays.toString(proceedingJoinPoint.getArgs()));
        operateLog.setReturnValue(proceed !=null?proceed.toString():"");
        operateLog.setCostTime(end-start);
        operateLogMapper.insert(operateLog);
        return proceed;
    }
}
